package com.zerosky.crm.util;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.zerosky.crm.po.Customer;
import com.zerosky.crm.po.Employee;

/**
 * 用于检测用户是否登陆的过滤器，如果未登录，则重定向到指的登录页面
 * <p>
 * 配置参数
 * <p>
 * checkSessionKey 需检查的在 Session 中保存的关键字<br/>
 * redirectURL 如果用户未登录，则重定向到指定的页面，URL不包括 ContextPath<br/>
 * notCheckURLList 不做检查的URL列表，以分号分开，并且 URL 中不包括 ContextPath<br/>
 */
public class CheckLoginFilter implements Filter {

	protected FilterConfig filterConfig = null;

	public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain)
			throws IOException, ServletException {
		HttpServletRequest request = (HttpServletRequest) servletRequest;
		HttpServletResponse response = (HttpServletResponse) servletResponse;
		HttpSession session = request.getSession();

		Employee sessionKey = (Employee) session.getAttribute("loginUser");
		Customer sessionCustomer = (Customer) session.getAttribute("loginCustomer");
		String url = request.getContextPath() + "/homePage.do";
		String wxUrl = request.getContextPath() + "/weixin/UserAccess.do";

		String path = request.getRequestURI();
//		System.out.println("====url===="+path);
//		System.out.println(path);
		
		// 添加不被拦截的action
		if (path.indexOf("homePage.do") > -1 || path.indexOf("login.do") > -1 || path.indexOf("main.do") > -1
				|| path.indexOf("validateCode.do") > -1 || path.indexOf("loginOff.do") > -1
				|| path.indexOf("loginEnd.do") > -1
				|| path.indexOf("weixin/") > -1
				|| path.indexOf("news_detailed_body.do") > -1
				|| path.indexOf("editNews.do") > -1
				) {
			
			// 不限制的请求alltopic
			filterChain.doFilter(servletRequest, servletResponse); //
			return;
		} else if (path.indexOf(".do") > -1) {
			
			//微信客户进来的时候判定一下是否登录，如果没有登录去进行第一次登录操作。
//			if (sessionCustomer == null) {
//				String ua = ((HttpServletRequest) request).getHeader("user-agent").toLowerCase();
//				if (ua.indexOf("micromessenger") > 0) {// 是微信浏览器
//					if (path.indexOf("market_detailed_body.do")>-1||path.indexOf("insertInviteSign.do")>-1) {
//						//咨询
//						session.setAttribute("toUrl", path+"?"+request.getQueryString());
//						//System.out.println(path+"?"+request.getQueryString());
//					}
//					response.sendRedirect(wxUrl);
//					return;
//				}
//			}
			
			// 判定是否有session
			if (sessionKey == null && sessionCustomer==null) {
				response.sendRedirect(url);
				return;
			} else {
				filterChain.doFilter(servletRequest, servletResponse);
				return;
			}
		} else {
			filterChain.doFilter(servletRequest, servletResponse); //
			return;
		}
	}

	public void init(FilterConfig filterConfig) throws ServletException {

	}

	public void destroy() {

	}
}
